<template>
	<view>
		<navbar></navbar>
		<view class="container" v-if="isShow">
			<view class="Top_tilte">
				修改地址
			</view>
			<view class="item" v-for="(item,index) in addressList" :key="index">
				<view class="top">
					<view class="name">
						{{item.receiver}}
					</view>
					<view class="phone">
						{{item.receiver_phone}}
					</view>
				</view>
				<view class="address">
					<view class="text twoOver">
						{{item.format_detail}}
					</view>
					<view class="icon" @click="nav_Data(item.address_id)">
						<img src="@/static/images/user/address.png" alt="">
					</view>
				</view>
				<view class="operation">
					<view class="switch">
						设为默认
						<switch :checked="item.is_default == 1 ? true:false" :disabled="item.is_default == 1 ? true:false"  style="margin-left: 30rpx;" @change="isSwitch" @click="ismodify(item,index)"/>
					</view>
					<view class="delete" @click="Delete(item.address_id)">
						删除
					</view>
				</view>
			</view>
		</view>
		<view class="lack" v-else>
			<view class="yoshop-notcont" style="padding: 0;">
				<text class="iconfont icon-wushuju" style="color: #636363;"></text>
			</view>
			<view class="title">
				您暂时没有添加的地址
			</view>
		</view>
		<view class="bottom">
			<view class="btn" @click="nav_add">
				添加地址
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				addressList:[],//
				isShow:false,
				checked:false,//按钮状态
				
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
		this.getAddress()
		},
		onPullDownRefresh() { 
			
		},
		methods: {
		
			//获取地址列表
			getAddress(){
				let _this = this
				app._post_form('JadeAddress/lists', {}, function(result) {
					if (1 == result.code) {

						
						if(result.data.list.length > 0){
							_this.isShow = true
							_this.addressList = result.data.list
						}else{
							_this.isShow = false
						}
						
					}
				}, null, function() {
					// 解除按钮禁用
					app.showError(result.msg);
				});
			},
			//删除地址
			Delete(e){
				let _this = this
				app._post_form('JadeAddress/delete', {
					address_id:e
				}, function(result) {
					if (1 == result.code) {
				    app.showToast(result.msg);
					_this.getAddress()
						
					}
				}, null, function() {
					// 解除按钮禁用
					app.showError(result.msg);
				});
			},
			//修改默认地址
			Modify(e){
				let _this = this
				app._post_form('JadeAddress/setDefault', {
					address_id:e
				}, function(result) {
					if (1 == result.code) {
				    app.showToast(result.msg);
					_this.getAddress()
						
					}
				}, null, function() {
					// 解除按钮禁用
					app.showError(result.msg);
				});
			},
			//添加
			nav_add(){
				uni.redirectTo({
					url: `./add`
				})
			},
			//修改
			nav_Data(e){
				uni.redirectTo({
					url: `./addData?address_id=${e}`
				})
			},
			//修改默认
			isSwitch(e){
				console.log(e)
				this.checked = e.detail.value
				// if(e.detail.value){
				// 	this.addressList.forEach((item)=>{
				// 		item.is_default = 0
				// 	})
				// 	e.is_default = 1
				// }else{
				// 	e.is_default = 1
				// 	   app.showToast('不能取消默认地址哦~');
				// }
				this.$forceUpdate()
			},
			ismodify(e,i){
				console.log(e,i)
				let _this = this
				if(e.is_default == 1){
					 app.showToast('不能取消默认地址哦~');
				}else{
					_this.addressList.forEach((item,index)=>{
						if(index == i){
							item.is_default = 1
							_this.Modify(item.address_id)
						}else{
							item.is_default = 0
						}
							
					})
				}
				
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #e7e7e7;
	}
	.lack{
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		    justify-content: center;
		    align-items: center;
			letter-spacing: 2rpx;
			
			.icon{
				width: 300rpx;
				height: 300rpx;
				text{
					width: 100%;
					height: 100%;
				}
			}
			.title{
				font-size: 40rpx;
				font-weight: bold;
				color: #333333;
			}
	}
	.Top_tilte {
		margin-top: 40rpx;
		width: 100%;
		height: 80rpx;
		background-color: #FFFFFF;
		text-align: center;
		color: rgba(0, 0, 0, 0.9);
		font-size: 30rpx;
		font-family: 'dy-iconfont';
		font-weight: bold;
		line-height: 80rpx;
	}
	.container{
		padding-bottom: 300rpx;
		
		.item{
			margin: 0 16rpx;
			box-sizing: border-box;
			// height: 342rpx;
			margin-top: 16rpx;
			padding: 16rpx;
			background-color: #fff;
			border-radius: 10rpx;
			.top{
				margin-top: 40rpx;
				font-size: 34rpx;
				font-weight: bold;
				color: #333333;
				display: flex;
				.name{
					margin-right: 10rpx;
				}
			}
			.address{
				margin-top: 24rpx;
				padding-bottom: 10rpx;
				display: flex;
				justify-content: space-between;
				    align-items: center;
					
					border-bottom: 1px solid rgba(51,51,51,0.0600);
				.text{
					width: 542rpx;
					height: 120rpx;
					line-height: 60rpx;
					font-size: 34rpx;
					font-weight: 500;
					color: #999999;
				}
				.icon{
					width: 48rpx;
					height: 48rpx;
					img{
						width: 100%;
						height: 100%;
					}
				}
			}
			.operation{
				margin-top: 15rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.delete{
					font-size: 28rpx;
					font-weight: 500;
					color: #FF602E;
				}
			}
		}
	}
	.bottom{
		position: fixed;
		bottom: 60rpx;
		left: 0;
		width: 100%;
		box-shadow: 0rpx -1rpx 8rpx 0rpx rgba(0,0,0,0.0600);
		.btn{
			margin: 16rpx 28rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #32393F linear-gradient(88deg, #4C5660 0%, #32383F 100%);
			box-shadow: 0rpx 8rpx 8rpx 0rpx rgba(73,167,255,0.2400);
			border-radius: 4rpx;
			border-radius: 8rpx;
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
		}
	}
</style>
